home *** CD-ROM | disk | FTP | other *** search
- /*==================================================================
- File: MacZString.h
-
- Contains: Mac-specific parsing for ZStrings.
-
- Written by: Eric Traut
-
- Copyright: 2000-2001 Connectix Corporation
-
- This source has been placed into the public domain by
- Connectix Corporation. You have the right to modify,
- distribute or use this code without any legal limitations
- or finanicial/licensing requirements. Connectix is not
- liable for any problems that result from the use of this
- code.
-
- If you have comments, feedback, questions, or would like
- to submit bug fixes or updates to this code, please email
- opensource@connectix.com.
- ==================================================================*/
-
- #include <Script.h>
-
- #include "MacZString.h"
- #include "ZStringDictionary.h"
-
- Handle MacZString::sOverrideHandle = NULL;
-
-
- /*------------------------------------------------------------------
- Initialize [static]
-
- This static method instantiates the singleton classes
- for tracking ZStrings.
- ------------------------------------------------------------------*/
-
- void
- MacZString::Initialize()
- {
- new ZStringDictionary();
- new MacZStringParser();
-
- sOverrideHandle = NULL;
- }
-
-
- /*------------------------------------------------------------------
- TearDown [static]
- ------------------------------------------------------------------*/
-
- void
- MacZString::TearDown()
- {
- delete &ZStringDictionary::GetZStringDictionary();
- delete &ZStringParser::GetZStringParser();
-
- // Clean up the previously detached override dictionary
- if (sOverrideHandle != NULL)
- ::DisposeHandle(sOverrideHandle);
- }
-
-
- /*------------------------------------------------------------------
- GetNamedPString [static]
- ------------------------------------------------------------------*/
-
- void
- MacZString::GetNamedPString(
- const char * inNamedString,
- StringPtr outPString,
- Z_UInt32 inMaxLength,
- Z_Boolean inDataIsVolatile)
- {
- ZString namedString;
-
- check(inMaxLength <= 255);
- namedString.GetNamedString(inNamedString, inDataIsVolatile);
- namedString.GetPString(outPString, inMaxLength + 1);
- }
-
-
- /*------------------------------------------------------------------
- GetNamedPString [static]
- ------------------------------------------------------------------*/
-
- void
- MacZString::GetNamedPString(
- ConstStringPtr inNamedString,
- StringPtr outPString,
- Z_UInt32 inMaxLength,
- Z_Boolean inDataIsVolatile)
- {
- char namedCString[256];
- ZString namedString;
-
- check(inMaxLength <= 255);
-
- // Copy the input Pascal string into a temporary C string buffer.
- std::memcpy(namedCString, &inNamedString[1], inNamedString[0]);
- namedCString[inNamedString[0]] = '\0';
-
- namedString.GetNamedString(namedCString, inDataIsVolatile);
- namedString.GetPString(outPString, inMaxLength + 1);
- }
-
-
- /*------------------------------------------------------------------
- LoadOverrideDictionary
- ------------------------------------------------------------------*/
-
- void
- MacZString::LoadOverrideDictionary(
- ResID inOverrideID)
- {
- sOverrideHandle = ::Get1Resource(kZStringOverrideDictionaryResType, inOverrideID);
-
- if (sOverrideHandle != NULL)
- {
- // Detach the resource so that standalone code can close
- // the resource file containing the override dictionary.
- ::DetachResource(sOverrideHandle);
-
- // The resource must remain locked in place because
- // dictionary entries will continue to point to
- // these named strings.
- ::MoveHHi(sOverrideHandle);
- ::HLock(sOverrideHandle);
- ZString::PopulateDictionary((char *)*sOverrideHandle);
-
- Handle twoByteTable;
- twoByteTable = ::Get1Resource(kZStringTwoByteTableResType, inOverrideID);
-
- // Override the two-byte table if necessary.
- if (twoByteTable != NULL)
- {
- ::HLock(twoByteTable);
- ZStringParser::GetZStringParser().OverrideTwoByteTable((UInt8 *)*twoByteTable);
- ::ReleaseResource(twoByteTable);
- }
- }
- }
-
-
- /*------------------------------------------------------------------
- CalculateFontInfoBaseID [static]
-
- This routine calculates the base resource ID of our 'finf'
- resources. Each language gets up to 16 'finf', and they start
- at a base of 128.
- ------------------------------------------------------------------*/
-
- ResID
- MacZString::CalculateFontInfoBaseID(
- ResID inOverrideID)
- {
- return (inOverrideID - kEnglishOverrideID) * 16 + 128;
- }
-
-
- /*------------------------------------------------------------------
- CalcOverrideDictionaryID [static]
-
- This routine attempts to determine which override dictionary
- the application should use.
- ------------------------------------------------------------------*/
-
- ResID
- MacZString::CalcOverrideDictionaryID()
- {
- // First, see if there's a a 'ZLan' resource.
- Handle lanSpecResource;
- ResID dictionaryID = 0;
-
- lanSpecResource = ::Get1Resource(kZStringLanguageSpecifierResType, kZStringLanguageSpecifierID);
- if (lanSpecResource != NULL)
- {
- if (::GetHandleSize(lanSpecResource) >= sizeof(UInt16))
- dictionaryID = *(UInt16 *)*lanSpecResource;
-
- ::ReleaseResource(lanSpecResource);
- }
-
- if (dictionaryID == 0)
- {
- // We didn't find a specific language resource,
- // so we need to attempt to determine the ID from
- // the system software.
-
- SInt32 languageCode;
-
- // We'll get the language for the system script.
- // fix me - should we use the "current script" instead?
- languageCode = ::GetScriptVariable(smSystemScript, smScriptLang);
-
- switch (languageCode)
- {
- default:
- case langEnglish:
- dictionaryID = kEnglishOverrideID;
- break;
-
- case langFrench:
- dictionaryID = kFrenchOverrideID;
- break;
-
- case langGerman:
- dictionaryID = kGermanOverrideID;
- break;
-
- case langItalian:
- dictionaryID = kItalianOverrideID;
- break;
-
- case langSpanish:
- dictionaryID = kSpanishOverrideID;
- break;
-
- case langJapanese:
- dictionaryID = kJapaneseOverrideID;
- break;
-
- case langPortuguese:
- dictionaryID = kPortugueseOverrideID;
- break;
- }
- }
-
- return dictionaryID;
- }
-
-
- /*------------------------------------------------------------------
- ExtractMenuInfo [static]
- ------------------------------------------------------------------*/
-
- UInt8
- MacZString::ExtractMenuInfo(
- const char * inMenuZString,
- Str255 outMenuItemText)
- {
- ZString menuItemDescription;
- ZString menuItemText;
- ZString menuItemKey;
- UInt8 menuKey = 0;
-
- menuItemDescription.GetNamedString(inMenuZString);
-
- // Get the two substrings.
- menuItemText = menuItemDescription.GetSubstring(0, '/');
- menuItemKey = menuItemDescription.GetSubstring(1, '/');
-
- menuItemText.GetPString(outMenuItemText);
-
- if (menuItemKey.GetLength() > 0)
- {
- check(menuItemKey.GetLength() == 1);
- menuKey = menuItemKey.GetCString()[0];
- }
-
- return menuKey;
- }
-
-
- /*------------------------------------------------------------------
- BuildMenu [static]
- ------------------------------------------------------------------*/
-
- void
- MacZString::BuildMenu(
- MenuHandle inMenuHandle,
- const char ** inMenuInfo,
- Boolean inInsertMenu)
- {
- Str255 menuItemText;
- UInt8 menuKey;
- UInt32 infoIndex = 0;
- const char * curMenuItemInfo;
- UInt16 itemIndex = 1;
-
- while (inMenuInfo[infoIndex] != NULL)
- {
- curMenuItemInfo = inMenuInfo[infoIndex];
-
- // Is this a divider?
- if (curMenuItemInfo[0] == 0)
- {
- ::AppendMenu(inMenuHandle, "\p-");
- ::DisableMenuItem(inMenuHandle, itemIndex);
- }
- else
- {
- menuKey = ExtractMenuInfo(curMenuItemInfo, menuItemText);
-
- // Append the new item. We don't send the text directly
- // in to AppendMenu because it may interpret some of the
- // special characters as code for command keys, etc.
- ::AppendMenu(inMenuHandle, "\p ");
- ::SetMenuItemText(inMenuHandle, CountMenuItems(inMenuHandle), menuItemText);
-
- if (menuKey != 0)
- ::SetItemCmd(inMenuHandle, itemIndex, menuKey);
- }
-
- infoIndex++;
- itemIndex++;
- }
-
- // Insert into the menu bar at the end.
- if (inInsertMenu)
- ::InsertMenu(inMenuHandle, 0);
- }
-
-
- /*------------------------------------------------------------------
- ReplaceParameter
- ------------------------------------------------------------------*/
-
- ZString
- MacZString::ReplaceParameter(
- Z_UInt8 inParamNum,
- ConstStringPtr inString)
- {
- char cString[256];
-
- // Copy the input Pascal string into a temporary C string buffer.
- std::memcpy(cString, &inString[1], inString[0]);
- cString[inString[0]] = '\0';
-
- return ZString::ReplaceParameter(inParamNum, cString);
- }
-
- /*------------------------------------------------------------------
- GetTagReplacement
- ------------------------------------------------------------------*/
-
- UInt16
- MacZStringParser::GetTagReplacement(
- ZStringTagID inTagID,
- char * outReplacement)
- {
- // By default, all tags are one character.
- UInt16 tagSize = 1;
-
- if (outReplacement != NULL)
- {
- static const char sTagReplacements[] = {
- '\0', // kTagIDInvalid
- 0xAE, // kZTag_AElig,
- 0xE7, // kZTag_Aacute,
- 0xE5, // kZTag_Acirc,
- 0xCB, // kZTag_Agrave,
- 0x81, // kZTag_Aring,
- 0xCC, // kZTag_Atilde,
- 0x80, // kZTag_Auml,
- 0x82, // kZTag_Ccedil,
- 0x83, // kZTag_Eacute,
- 0xE6, // kZTag_Ecirc,
- 0xE9, // kZTag_Egrave,
- 0xE8, // kZTag_Euml,
- 0xEA, // kZTag_Iacute,
- 0xEB, // kZTag_Icirc,
- 0xED, // kZTag_Igrave,
- 0xEC, // kZTag_Iuml,
- 0x84, // kZTag_Ntilde,
- 0xEE, // kZTag_Oacute,
- 0xEF, // kZTag_Ocirc,
- 0xF1, // kZTag_Ograve,
- 0xAF, // kZTag_Oslash,
- 0xCD, // kZTag_Otilde,
- 0x85, // kZTag_Ouml,
- 0xF2, // kZTag_Uacute,
- 0xF3, // kZTag_Ucirc,
- 0xF4, // kZTag_Ugrave,
- 0x86, // kZTag_Uuml,
- 0x87, // kZTag_aacute,
- 0x89, // kZTag_acirc,
- 0xBE, // kZTag_aelig,
- 0x88, // kZTag_agrave,
- 0x26, // kZTag_amp,
- 0x8C, // kZTag_aring,
- 0x8B, // kZTag_atilde,
- 0x8A, // kZTag_auml,
- 0xE3, // kZTag_bdquo,
- 0xA5, // kZTag_bull,
- 0x8D, // kZTag_ccedil,
- 0xA2, // kZTag_cent,
- 0xA9, // kZTag_copy,
- 0x8E, // kZTag_eacute,
- 0x90, // kZTag_ecirc,
- 0x8F, // kZTag_egrave,
- 0x91, // kZTag_euml,
- 0x3E, // kZTag_gt,
- 0xC9, // kZTag_hellip,
- 0x92, // kZTag_iacute,
- 0x94, // kZTag_icirc,
- 0xC1, // kZTag_iexcl,
- 0x92, // kZTag_igrave,
- 0xC0, // kZTag_iquest,
- 0x95, // kZTag_iuml,
- 0xD2, // kZTag_ldquo,
- 0xD4, // kZTag_lsquo,
- 0x3C, // kZTag_lt,
- 0xD1, // kZTag_mdash,
- 0xB5, // kZTag_micro,
- 0xCA, // kZTag_nbsp,
- 0xD0, // kZTag_ndash,
- 0x96, // kZTag_ntilde,
- 0x97, // kZTag_oacute,
- 0x99, // kZTag_ocirc,
- 0x98, // kZTag_ograve,
- 0xBF, // kZTag_oslash,
- 0x9B, // kZTag_otilde,
- 0x9A, // kZTag_ouml,
- 0xA6, // kZTag_para,
- 0xB9, // kZTag_pi,
- 0xA3, // kZTag_pound,
- 0xD3, // kZTag_rdquo,
- 0xA8, // kZTag_reg,
- 0x00, // kZTag_replace
- 0xD5, // kZTag_rsquo,
- 0xE2, // kZTag_sbquo,
- 0xA7, // kZTag_szlig,
- 0xAA, // kZTag_trade,
- 0x9C, // kZTag_uacute,
- 0x9E, // kZTag_ucirc,
- 0x9D, // kZTag_ugrave,
- 0x9F, // kZTag_uuml,
- 0xB4, // kZTag_yen,
- 0xD8, // kZTag_yuml
- 0x0D // kZTag_br
- };
-
- check(inTagID > kZTag_Invalid && inTagID < sizeof(sTagReplacements));
- check(inTagID != kZTag_replace);
-
- // fill in the equivalent
- *outReplacement = sTagReplacements[inTagID];
- }
-
- return tagSize;
- }
-
-
-
-
-